#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>
#include<unordered_map>

using namespace std;

long long countPairs(int n, vector<vector<int>>& edges) {
    vector<int>  uf(n, -1);
    auto findroot = [&](int i)
    {
        while (uf[i] != -1)
            i = uf[i];
        return i;
    };

    for (int i = 0; i < edges.size(); i++)
    {
        int root1 = findroot(edges[i][0]);
        int root2 = findroot(edges[i][1]);
        if (root1 != root2)
            uf[root2] = root1;
    }

    unordered_map<int, int> um;
    for (int i = 0; i < n; i++)
    {
        int root1 = findroot(i);
        um[root1]++;
    }

    long long res = 0;
    for (int i = 0; i < n; i++)
    {
        int root = findroot(i);
        if (root == -1)
            root = i;
        res += n - um[root];
    }

    return res / 2;
}

int main()
{
    //vector<vector<int>> vv = { {0, 1},{0, 2},{1, 2} };

    vector<vector<int>> vv = { {0, 2},{0, 5},{2, 4},{1, 6},{5, 4} };
    cout << countPairs(7, vv);
	return 0;
}